package com.base.demo;

import org.junit.Test;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class LockService {

    private static JedisPool pool = null;
    private static LockRedis lockRedis = null;

    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(200); //设置最大连接数
        config.setMaxIdle(8); //设置最大空闲数
        config.setMaxWaitMillis(1000*100); //设置最大等待时间
        config.setTestOnBorrow(true); //在borrow 一个jedis 实例，是否需要验证，若为true, 则所有的jedis实例均为可用
        pool = new JedisPool(config,"127.0.0.1", 6379, 3000);

        lockRedis = new LockRedis(pool);
    }


    public void seckill() {
        String value = lockRedis.getRedisLock(5000l, 5000l);
        if (value == null) {
            System.out.println(Thread.currentThread().getName() + "获取锁失败，获取时间超时");
            return;
        }
        System.out.println(Thread.currentThread().getName() + "获取锁成功");

        //释放锁
        lockRedis.unRedisLock(value);
    }

}
